<?php 
/**
 * 后台Admin模块 幼儿园班级管理功能
 * 
 */
#定义命名空间
namespace Admin\Controller;

class ClassController extends ComController {

    /**
     * 班级列表
     * @return [type] [description]
     */
    public function index(){
        //获取当前登录用户所属的学校id
        $sc_id = session('sc_id');
        $class = D('class');
        $where['sc_id'] = $sc_id;
        // $where['is_del'] = 1; //未删除
        $prefix = C('DB_PREFIX');//读取配置文件 数据表前缀
        $p = I('get.p') ? I('get.p') : 1;
        $pageSize = 15; //每页显示条数
        $limit = ($p-1)*$pageSize; //计算偏移量
        $order = ' id desc';
        $count = $class->where($where)->count();
        $class_info = $class->where($where)->order($order)->limit($limit.','.$pageSize)->select();
        //组装数据 老师名称
        $data = array();
        if($class_info){

            foreach ($class_info as $k=> $v) {
                $data[$k]['id'] = $v['id'];
                $data[$k]['name'] = $v['name'];
                $data[$k]['desc'] = $v['desc'];
                $data[$k]['class_type'] = $this->classType($v['class_type']);
                $data[$k]['num'] = $v['num'];
                $data[$k]['sign_num'] = $v['sign_num'];
                $data[$k]['price'] = $v['price'];
                $data[$k]['add_time'] = $v['add_time'];
                $data[$k]['sc_id'] = $v['sc_id'];
                $data[$k]['is_del'] = $v['is_del'];
                $data[$k]['class_manager'] = $this->byIdToTeacher($v['class_manager']);
                $data[$k]['class_teacher'] = $this->byIdToTeacher($v['class_teacher']);
                $data[$k]['life_teacher'] = $this->byIdToTeacher($v['life_teacher']);
            }
        }
        $page = new \Think\Page($count,$pageSize);
        $page = $page->show();
        $this->assign('page',$page);
        $this->assign('class',$data);
        $this->display();
    }

    /**
     * 添加班级信息
     */
    public function append(){
        //获取当前登录用户所属的学校id
        $sc_id = session('sc_id');
        if(IS_POST){
            //接收参数 验证
            $class = D('class');
            $param = I('post.');
            $param['sc_id'] = $sc_id;
            $param['add_time'] = time();
            $res = $class->create($param);
            if($res){
                //检测名称是否已被使用
                $where['sc_id'] = $sc_id;
                $where['name'] = I('post.name');
                $isNameSet = $class->where($where)->find();
                if(!$isNameSet){

                    $class_res = $class->add($param);
                    if($class_res){

                        $this->success('添加成功!',U('index'));
                    }else{
                        $this->error('添加失败!');
                    }
                }else{
                    $this->error('班级名称已经存在，不能重复添加!');
                }
            }else{
                $this->error($class->getError());
            }
        }else{
            //展示页面
            $class_type = M('class_type');//实例化班级级别
            $where['sc_id'] = $sc_id;
            $where['is_del'] = 1;//未删除
            $type = $class_type->where($where)->field('id,name')->select();
            $this->assign('class_type',$type);
            $this->assign('form_url',U('append'));
            $this->display();
        }
    }

    /**
     * 修改班级信息
     * @return boolean 成功返回true 失败返回false
     */
    public function edit(){
        //获取当前登录用户所属的学校id
        $sc_id = session('sc_id');
        $class = D('class');//实例化模型
        $id = I('get.id') ? I('get.id') : 0 ;
        if(IS_POST){
            //接收参数 验证
            $param = I('post.');
            $param['update_time'] = time();
            $res = $class->create($param);
            if($res){
                $id = $param['id'];
                $name = $param['name'];
                $where['id'] = $id;
                unset($param['id']);
                //判断名称是否修改
                $class_data = $class->where($where)->find();
                if($class_data['name'] != $name){
                    //检测名称是否已被使用
                    $where['sc_id'] = $sc_id;
                    $where['name'] = I('post.name');
                    $isNameSet = $class->where($where)->find();
                    if(!$isNameSet){

                        $class_res = $class->where('id = '.$id)->save($param);
                        if($class_res !== false ){

                            $this->success('修改成功!',U('index'));
                        }else{
                            $this->error('修改失败!');
                        }
                    }else{
                        $this->error('班级名称已经存在，不能重复修改!');
                    }

                }else{
                    $class_res = $class->where('id = '.$id)->save($param);
                    if($class_res !== false ){

                        $this->success('修改成功!',U('index'));
                    }else{
                        $this->error('修改失败!');
                    }
                }                
            }else{
                $this->error($class->getError());
            }
        }else{
            //展示页面
            if($id){
                $where['id'] = $id;
                $class_info = $class->where($where)->find();
                $class_type = M('class_type')->where(' is_del = 1 and sc_id ='.$sc_id)->select();
                $this->assign('class',$class_info);
                $this->assign('class_type',$class_type);
                $this->assign('form_url',U('edit'));
                $this->display('append');
            }else{
                $this->error('缺少参数!',U('index'));
            }
        }
    }

    /**
     * 删除班级
     * @return  boolean 成功返回true 失败返回false
     */
    public function del(){
        $id = I('get.id') ? I('get.id') : 0 ;//获取主键id   
        if($id){
            $class = D('class');
            $where['id'] = $id;
            if($this->is_del($id)){
                $res = $class->where($where)->delete();
                if($res){
                    $this->error('删除成功!',U('index'));
                }else{
                    $this->error('删除失败!',U('index'));
                }
            }else{
                $this->error('班级信息下有关联信息，不能删除!',U('index'));
            }
        }else{
            $this->error('缺少参数!');
        }
    }

    /**
     * ajax修改显示状态
     * @param string $is_show 课程表主键id-状态 11-1 11-0
     */
    public function is_show(){
        $param = I('post.is_show') ? I('post.is_show') : 0;//获取id
        $param = explode('-', $param);
        $is_show = $param['1'];
        $id = $param['0'];
        if($id){
            $class = M('class');
            $class_data= $class->where('id = '.$id)->find();//实例化课程主表
            if($class_data){
                $class_res = $class->where(' id = '.$id)->save(array('is_del'=>$is_show));
                if($class_res !== false){
                    echo json_encode(array('res'=>1,'msg'=>'成功'));die;
                }else{
                    echo json_encode(array('res'=>0,'msg'=>'失败'));die;
                }
            }else{
                echo json_encode(array('res'=>0,'msg'=>'失败，没有数据'));die;
            }
        }else{
            echo json_encode(array('res'=>0,'msg'=>'失败，没有参数'));die;
        }
    }

    /**
     * ajax根据班级级别 获取教师信息
     * http post请求
     */
    public function getTeacher(){

        $class_id = I('post.class_id') ? I('post.class_id') : 0; //获取参数
        if($class_id){
            //获取当前登录用户的学校id
            $sc_id = session('sc_id');
            $where['pid'] = $sc_id;
            $where['class_id'] = $class_id;
            $where['level'] = 2;
            $member = M('member');//实例化教师|幼儿园表
            $teacher = $member->where($where)->field('uid,name,class_id,teacher_type')->select();
            if($teacher){
                echo json_encode(array('res'=>1,'data'=>$teacher,'msg'=>'查询成功'));
            }else{
                echo json_encode(array('res'=>0,'data'=>'','msg'=>'查询失败，没有数据'));
            }
        }else{
            echo json_encode(array('res'=>0,'data'=>'','msg'=>'查询失败，缺少参数'));
        }
    }

    /**
     * 根剧教师id获取教师信息
     */
    public function byIdToTeacher($id = 0){
        if($id){
            $member = M('member');//实例化教师表
            $data = $member->field('uid,name,class_id,teacher_type,user,pid')->where('uid = '.$id)->find();
            if($data){
                return $data;
            }else{
                return '';
            }
        }else{
            return '';
        }
    }

    //根ju班级类型id获取级别信息
    public function classType($id = 0){
        if($id){
            $member = M('class_type');//实例化教师表
            $data = $member->field('id,name')->where('id = '.$id)->find();
            if($data){
                return $data;
            }else{
                return '';
            }
        }else{
            return '';
        }
    }
    //查看该班级报名人员
    public function lookBody(){
        $class_id = I('get.id');
        $name = I('get.name');
        $data = M('baoming')
                ->field(" sc_baoming.*,u.name as u_name,b.body_name as b_name ")
                ->join(" sc_user as u on u.id=sc_baoming.user_id ","LEFT")
                ->join(" sc_body as b on b.id=sc_baoming.body_id ","LEFT")
                ->where(array('class_id'=>$class_id,'is_pay'=>1))
                ->order('sc_baoming.add_time desc')
                ->select();
        $this->assign('name',$name);
        $this->assign('data',$data);
        $this->display();
    }

    /**
     * 根据班级id 查看是否有报名信息、等跟班级有关联的信息
     * 如有则不能删除 否则 可以删除
     * @return boolean 返回true则可以删除 返回false 不能删除
     */
    public function is_del($id=0){
        $sc_id = session('sc_id');
        $course = M('course');
        $baoming = M('baoming');
        $where['sc_id'] = $sc_id;
        $where['class_id'] = $id;
        $c_res = $course->where($where)->select();
        $b_res = $baoming->where($where)->select();
        if($c_res || $b_res){
            return false;
        }else{
            return true;
        }
    }
}